//
//  BVTCell.m
//  BuyVegetable
//
//  Created by zhuzi on 16/10/27.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVMyCollectionCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+Extension.h"
@implementation BVMyCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setUI];
    }
    return self;
    
}
- (void)setUI{
    
    // 单选按钮
    self.radioButton = [[UIButton alloc] init];
    [self.radioButton setImage:IMAGE_NAMED(@"huiduihao") forState:UIControlStateNormal];
    [self.radioButton setImage:IMAGE_NAMED(@"hongduihao") forState:UIControlStateSelected];
    [self.radioButton addTarget:self action:@selector(radioClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.radioButton];
    
    // 配图
    self.iconImageV = [[UIImageView alloc] init];
    self.iconImageV.layer.masksToBounds = YES;
    self.iconImageV.layer.cornerRadius = 5;
    self.iconImageV.layer.borderWidth = .5f;
    self.iconImageV.layer.borderColor = [UIColor BVTextLightGrayColor].CGColor;
    [self.contentView addSubview:self.iconImageV];
    
    // 标题
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textColor = [UIColor BVBaseTextBlackColor];
    self.nameLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.contentView addSubview:self.nameLabel];
    
    // 促销
    self.salesLabel = [[UILabel alloc] init];
    self.salesLabel.layer.masksToBounds = YES;
    self.salesLabel.layer.borderWidth = 1;
    self.salesLabel.layer.borderColor = [UIColor cyanColor].CGColor;
    self.salesLabel.textColor = [UIColor cyanColor];
    self.salesLabel.textAlignment = NSTextAlignmentCenter;
    self.salesLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.salesLabel];
    
    // 删除
    self.deleteButton = [[UIButton alloc] init];
    [self.deleteButton setBackgroundImage:IMAGE_NAMED(@"shanchu") forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.deleteButton];
    
    // 单价
    self.unitPriceLabel = [[UILabel alloc] init];
    self.unitPriceLabel.textColor = [UIColor BVBaseTextBlackColor];
    self.unitPriceLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.unitPriceLabel];
    
    // 重量
    self.weightLabel = [[UILabel alloc] init];
    self.weightLabel.font = [UIFont systemFontOfSize:14];
    self.weightLabel.textColor = [UIColor BVBaseOrangeColor];
    [self.contentView addSubview:self.weightLabel];
    
    // 小计
    self.totalLabel = [[UILabel alloc] init];
    self.totalLabel.font = [UIFont boldSystemFontOfSize:15];
    self.totalLabel.textColor = [UIColor BVBaseOrangeColor];
    [self.contentView addSubview:self.totalLabel];
    
    // 减
    self.minusButton = [[UIButton alloc] init];
    self.minusButton.enabled = NO;
    [self.minusButton setBackgroundImage:IMAGE_NAMED(@"jianhao_1") forState:UIControlStateNormal];
    [self.contentView addSubview:self.minusButton];
    
    // 数量
    self.numLabel = [[UILabel alloc] init];
    self.numLabel.text = @"1";
    self.numLabel.textColor = [UIColor BVBaseTextBlackColor];
    self.numLabel.backgroundColor = [UIColor BVLineGrayColor];
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    self.numLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.numLabel];
    
    // 加
    self.addButton = [[UIButton alloc] init];
    [self.addButton setBackgroundImage:IMAGE_NAMED(@"jiahao_1") forState:UIControlStateNormal];
    [self.contentView addSubview:self.addButton];
    
    // 限购提示
    self.limitLabel = [[UILabel alloc] init];
    self.limitLabel.numberOfLines = 0;
    self.limitLabel.font = [UIFont systemFontOfSize:14];
    self.limitLabel.textColor = [UIColor BVBaseTextBlackColor];
    self.limitLabel.backgroundColor = COLOR_RGB(255, 207, 191);
    [self.contentView addSubview:self.limitLabel];
    
     //间距
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor BVBackgroundLightGrayColor];
    [self.contentView addSubview:lineView];
    
    CGFloat margin = 0;
    UIView *contentView = self.contentView;
    
    self.radioButton.sd_layout
    .centerYEqualToView(contentView)
    .leftSpaceToView(contentView,10)
    .widthIs(30)
    .heightIs(40);

    self.iconImageV.sd_layout
    .topSpaceToView(contentView,10)
    .leftSpaceToView(contentView, 50)
    .heightIs(80)
    .widthEqualToHeight();
    
    self.limitLabel.sd_layout
    .leftSpaceToView(contentView, 50)
    .topSpaceToView(self.iconImageV, 5)
    .rightSpaceToView(contentView, 10)
    .autoHeightRatio(0);
    [self setupAutoHeightWithBottomView:self.limitLabel bottomMargin:5];
    

    self.nameLabel.sd_layout
    .topSpaceToView(contentView,10)
    .leftSpaceToView(self.iconImageV,10)
    .heightIs(20);
    [self.nameLabel setSingleLineAutoResizeWithMaxWidth:180];
    
    self.salesLabel.sd_layout
    .topSpaceToView(contentView,10)
    .leftSpaceToView(self.nameLabel,5)
    .heightIs(20)
    .widthIs(40);
    
    self.unitPriceLabel.sd_layout
    .topSpaceToView(self.nameLabel,5)
    .leftSpaceToView(self.iconImageV,10)
    .autoHeightRatio(0);
    [self.unitPriceLabel setSingleLineAutoResizeWithMaxWidth:180];
    
    self.weightLabel.sd_layout
    .topSpaceToView(self.nameLabel,5)
    .leftSpaceToView(self.unitPriceLabel,10)
    .heightIs(20)
    .widthIs(80);
    
    self.totalLabel.sd_layout
    .topSpaceToView(self.unitPriceLabel,20)
    .leftSpaceToView(self.iconImageV,10)
    .autoHeightRatio(0)
    .widthIs(60);
    
    _deleteButton.sd_layout
    .topSpaceToView(contentView,10)
    .rightSpaceToView(contentView,10)
    .heightIs(20)
    .widthEqualToHeight();
    
    lineView.sd_layout
    .topSpaceToView(self.limitLabel,5)
    .leftSpaceToView(self.contentView,0)
    .rightSpaceToView(self.contentView,0)
    .heightIs(2);
    
    // 当你不确定哪个view在自动布局之后会排布在cell最下方的时候可以调用次方法将所有可能在最下方的view都传过去
   [self setupAutoHeightWithBottomViewsArray:@[self.limitLabel, self.iconImageV,lineView] bottomMargin:margin];
    
}
#pragma mark -
#pragma mark - 刷新数据 - 真数据
- (void)setMycollectionModel:(BVMycollectionModel *)mycollectionModel{
    
    _mycollectionModel = mycollectionModel;
    
    //商品 图片
    [self.iconImageV sd_setImageWithURL:[NSURL URLWithString:self.mycollectionModel.imgUrl] placeholderImage:IMAGE_NAMED(@"placeholderImage.png")];
    // 商品名字
    self.nameLabel.text = self.mycollectionModel.title;
    // 单价 和 规格
    self.unitPriceLabel.text = [NSString stringWithFormat:@"%@元/d袋",self.mycollectionModel.unit_price];
    // 重量
    self.weightLabel.text = [NSString stringWithFormat:@"%@",self.mycollectionModel.weight];
    // 小计
    self.totalLabel.text = [NSString stringWithFormat:@"%@",self.mycollectionModel.all_price];
    // 注意 注释
    self.limitLabel.text = [NSString stringWithFormat:@"女子走夜路被抢劫女子走夜路被抢劫女子走夜路被抢劫%@",self.mycollectionModel.notice];
    // 是否是促销
    if ([self.mycollectionModel.is_sales isEqualToString:@"0"]) {
        [self.salesLabel setText:@"促销"];
    } else {
        [self.salesLabel setHidden:YES];
    }
    // 判断 是否 有效
    if ([self.shoppingCart.status isEqualToString:@"0"]) {
        [self.numLabel setHidden:YES];
        [self.addButton setHidden:YES];
        [self.minusButton setHidden:YES];
        [self.radioButton setImage:IMAGE_NAMED(@"shixiao") forState:UIControlStateNormal];
        [self.radioButton setEnabled:NO];
    } else {
        [self.numLabel setHidden:NO];
        [self.addButton setHidden:NO];
        [self.minusButton setHidden:NO];
        [self.radioButton setImage:IMAGE_NAMED(@"huiduihao") forState:UIControlStateNormal];
        [self.radioButton setEnabled:YES];
    }
}

#pragma mark -
#pragma mark - 单选
- (void)radioClick:(UIButton *)button {
    button.selected = !button.selected;
    if (self.radioBlock) {
        self.radioBlock(self.indexPath,button.selected);
    }
}
#pragma mark -
#pragma mark - 删除
- (void)deleteClick:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegateButClick:)]) {
        [_delegate delegateButClick:self.indexPath];
    }
}
- (void)showNumber:(NSInteger)count {
    [self.minusButton setEnabled:count == 1 ? NO : YES];
    self.numLabel.text = [NSString stringWithFormat:@"%lu",count];
    self.totalLabel.text = [NSString stringWithFormat:@"￥%.1f",count * [self.shoppingCart.unit_price floatValue]];
}
@end
